<!--
* @Author: 冯尚新 fengshangxin@yunjinhz.com
* @Date:  2023-05-19 10:20:06
* @FilePath: /src/pages/demo/ddsdk.vue
* @Description: 钉钉sdk调用示例
-->

<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<!-- 钉钉上不会显示theme-navbar -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
			<!-- 导航栏右侧搜索 -->
			<view class="nav-right-icon">
				<u-icon color="#FFF" name="search" size="38" @tap="handlerShowSearch"></u-icon>
			</view>
		</theme-navbar>
		<!-- #endif -->
		<u-button type="primary" @click="showToast">toast</u-button>
		<br />
		<u-button type="primary" @click="showAlert">alert</u-button>
		<br />
		<u-button type="primary" @click="setTitle">设置标题</u-button>
		<br />
		<u-button type="primary" @click="setRight">设置右侧单个按钮</u-button>
		<br />
		<u-button type="primary" @click="setRightMeun">设置右侧按钮组</u-button>
		<br />
		<u-button type="primary" @click="callPhone">拨打电话</u-button>
		<br />
		<u-button type="primary" @click="geolocation">获取定位</u-button>
		<br />
		<u-button type="primary" @click="previewImage">图片预览</u-button>
		<br />
		<u-button type="primary" @click="scanCode">扫码（二维码、条形码）</u-button>
		<br />
		<u-button type="primary" @click="scanQrCode">扫码（二维码）</u-button>
		<br />
		<u-button type="primary" @click="nfcReader">NFC读取</u-button>
		<br />
		<u-button type="primary" @click="navigateMap">打开指定位置</u-button>
		<br />
		<u-button type="primary" @click="clipboardData">复制到剪切板</u-button>
		<br />
		<u-button type="primary" @click="getLocationByMap">地图选点获取位置</u-button>
		<br />
		<u-button type="primary" @click="getLocationInMapBySearch">地图选点获取位置带search</u-button>
		<br />
		<u-button type="primary" @click="device">获取设备信息</u-button>
		<br />
		<u-button type="primary" @click="chooseImageToCompress">选择图片</u-button>
		<br />
		<u-button type="primary" @click="chooseWebview">weview集成</u-button>
		<br />
		<u-button type="primary" @click="chooseSign">签名</u-button>
		<image :src="base64"></image>
	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from 'image-tools'

	export default {
		data() {
			return {
				base64:""
			};
		},
		create() {
			document.addEventListener('optionMenu', function(e) {
				console.log(e)
				this.$deviceApi.toast("optionMenu clicked!");
			}, false);
			uni.$on('optionMenu', function(data) {
				console.log('监听到事件来自 update ，携带参数 msg 为：' + data.msg);
			})

		},
		onShow() {
			this.setRightMeun();
		},
		methods: {

			showToast(text) {
				this.$deviceApi.toast("1111");
			},
			showAlert(msg = '', title = '') {
				this.$deviceApi.alert("测试");
			},
			callPhone(phone) {
				this.$deviceApi.makePhoneCall("15829559290");
			},
			setTitle(title) {
				// #ifdef H5-DINGDING
				this.$deviceApi.setTitle("标题");
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持设置标题');
				// #endif
			},
			geolocation() {
				// #ifdef H5-DINGDING
				this.$deviceApi.geolocation().then((res) => {
					// 获取成功 这里是res 根据页面情况去获取对应的key
					//     {
					// 		 longitude : Number,
					// 		 latitude : Number,
					// 		 accuracy : Number,
					// 		 address : String,
					// 		 province : String,
					// 		 city : String,
					// 		 district : String,
					// 		 road : String,
					// 		 netType : String,
					// 		 operatorType : String,
					// 		 errorMessage : String,
					// 		 errorCode : Number,
					// 		 isWifiEnabled : Boolean,
					// 		 isGpsEnabled : Boolean,
					// 		 isFromMock : Boolean,
					// 		 provider : wifi|lbs|gps,
					// 		 accuracy : Number,
					// 		 isMobileEnabled : Boolean
					// 		}
				}).catch(err => {
					this.$u.toast('获取定位失败，请检查是否打开位置权限功能');
				});
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持获取定位');
				// #endif
			},
			previewImage(url = '', current = []) {
				// #ifdef H5-DINGDING
				this.$deviceApi.previewImage(url, current).then(res => {
					console.log('调用成功');
				}).catch(err => {
					console.log('调用失败');
				})
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持预览图片');
				// #endif
			},
			scanCode() {
				// #ifdef H5-DINGDING || H5-WECHAT || H5-EMP
				this.$deviceApi.scan().then(res => {
					this.$modal.alert('1' + JSON.stringify(res));
					// 这里res 直接返回 文字信息
				}).catch(err => {
					// 扫码失败
					console.log('扫码失败');
				})
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持扫码');
				// #endif
			},
			scanQrCode(type = 'all') {
				// type为qrCode(二维码)、barCode(条形码)、all(全部)
				// #ifdef H5-DINGDING || H5-WECHAT
				this.$deviceApi.barCode().then(res => {
					this.$modal.alert('2' + JSON.stringify(res));
					// 这里res 直接返回 文字信息
				}).catch(err => {
					// 扫码失败
				})
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持扫码');
				// #endif
			},
			nfcReader() {
				// #ifdef H5-DINGDING
				this.$deviceApi.nfcReader && this.$deviceApi.nfcReader().then(res => {
					// 这里res 直接返回 文字信息
				}).catch(err => {
					// 调用失败
				})
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持NFC');
				// #endif
			},
			navigateMap(lat, long, title) {
				/**
				 * @lat 必传 纬度
				 * @long 必传 精度
				 * @title 必传 标题
				 */
				// #ifdef H5-DINGDING
				this.$deviceApi.navigateMap(lat, long, title);
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持打开指定位置');
				// #endif
			},
			clipboardData(text = '') {
				/**
				 * @text 必传 粘贴的文字
				 */
				// #ifdef H5-DINGDING
				this.$deviceApi.clipboardData(text);
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持打开指定位置');
				// #endif
			},
			getLocationByMap(lat, lng, scope = 500) {
				/**
				 * @lat 必传 纬度
				 * @lng 必传 精度
				 * @scope 必传 延迟多少S
				 */
				// #ifdef H5-DINGDING
				this.$deviceApi.getLocationByMap(lat, lng, scope).then(res => {
					// 这里res直接返回
				}).catch(err => {
					// 调用失败
				})
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持地图选点获取位置');
				// #endif
			},
			getLocationInMapBySearch(lat, lng, scope = 500) {
				/**
				 * @lat 必传 纬度
				 * @lng 必传 精度
				 * @scope 必传 延迟多少S
				 */
				// #ifdef H5-DINGDING
				this.$deviceApi.getLocationInMapBySearch(lat, lng, scope).then(res => {
					// 这里res直接返回
				}).catch(err => {
					// 调用失败
				})
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持地图选点获取位置');
				// #endif
			},
			device() {
				// #ifdef H5-DINGDING
				this.$deviceApi.device().then(res => {
					// 这里res直接返回
				}).catch(err => {
					// 获取失败
				})
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持获取设备信息');
				// #endif
			},
			chooseImageToCompress(maxSize, Int) {
				/*
				 * @param {Object} maxSize 最多可以选择的图片张数，默认   1
				 * @param {Int} sType 0 默认，1 相册 ， 2 拍照
				 * @param {Number} quality   0-1
				 * @param {Number} widthScale 0-1
				 */
				// #ifdef H5-DINGDING
				// uni.chooseFile({
				// count: 1,
				// type:"image",
				// sizeType: ['original', 'compressed', ],
				// sourceType: ['camera'],
				// extension:['BMP',"JPG","JPEG","PNG"],
				// extension:['.png','.jpg',".JPEG",".png",".BMP",".bmp"],
				// success: res1 => {
				// uni.getImageInfo({
				// src: res1.tempFilePaths[0],
				// success: function (image) {
				// console.log(image.width);
				// console.log(image.height);
				// console.log(image.path);
				// console.log(image.type);
				// }
				// });
				//
				// },
				// fail: err => {
				// console.log(err)
				// },
				// });

				// showActionSheet("", [{
				// title: "拍照",
				// }, {
				// title: "从相册中选择",
				// }, ], "取消", function(res) {
				// console.log("chooseImageToCompress:",res);
				// imagePicker(res, maxSize);
				// })
				//

				dd.takePhoto().then(res => {
					this.$u.alert(JSON.stringify(res), "提示");
					console.log("chooseImageToCompress:", JSON.stringify(res));
					//
					let url = "file://" + res.path;
					console.log("path:", JSON.stringify(url));

					var MAX_HEIGHT = 800;
					var image = new Image();
					image.onload = function() {
						console.info("1" + image);
						console.info("image====" + image);
						var canvas = document.createElement("canvas");
						if (image.height > MAX_HEIGHT) {
							image.width *= MAX_HEIGHT / image.height;
							image.height = MAX_HEIGHT;
						}
						var ctx = canvas.getContext("2d");
						ctx.clearRect(0, 0, canvas.width, canvas.height);
						canvas.width = image.width;
						canvas.height = image.height;
						ctx.drawImage(image, 0, 0, image.width, image.height);
						var tmp = canvas.toDataURL();
						console.log("1111", tmp);
						// var imgbase64 = tmp.substr(22, tmp.length);
						// (<any>window).uexFileMgr.closeFile(id);
						// resolve(tmp);
					};
					image.src = filePath;









					// pathToBase64(url).then((res)=>{
					// console.log("res:",res);
					// }).catch((err)=>{
					// console.log("err:",res);
					// })

					// pathToBase64(res.path).then((res)=>{
					// console.log("res.path:",res);
					// }).catch((err)=>{
					// console.log("res.path:err:",res);
					// })

					// let img = new Image();
					// let canvas = document.createElement("canvas");
					// let ctx = canvas.getContext("2d");
					// img.src = path;
					// img.onload = function(){
					//
					// }
					// var request = new XMLHttpRequest();
					// request.onload = function() {
					// var file = new FileReader();
					//  file.onloadend = function() {
					// console.log("onloadend:",file.result);
					// callback(file.result);
					//  }
					// file.readAsDataURL(request.response);
					// };
					// request.open('GET', url);
					// request.responseType = 'blob';
					// request.send();

					// pathToBase64(url).then((res1)=>{
					// console.log("pathToBase64:",res1);
					// }).catch((err)=>{
					// console.log("pathToBase64:err",err);
					// });

				}).catch(err => {
					this.$u.toast(JSON.stringify(err));
				})

				// this.$deviceApi.chooseImageToCompress(maxSize, Int).then(res => {
				// 	// 这里res直接返回
				// }).catch(err => {
				// 	// 获取失败
				// })
				// #endif
				// #ifndef H5-DINGDING
				this.$u.toast('H5不支持图片选择');
				// #endif
			},
			setRight() {
				// #ifdef H5-DINGDING
				this.$deviceApi.setRight("预览", () => {
					this.setRight();
				}, true, true);
				// #endif
			},
			setRightMeun() {
				// this.$deviceApi.setRight("", () => {}, true, true);
				// 设置右侧多按钮，苹果手机对此接口的格式要求比较严格，不能出现多余的符号
				let menuItemStr = [{
					title: "预览",
					icon: ''
				}, {
					title: "修改",
					icon: ""
				}, {
					title: "备用",
					icon: ""
				}];
				// let menuItemStr = [{
				// 	icontype: 'scan',
				// 	title: '1111'
				// }, {
				// 	icontype: 'info',
				// 	title: '22222'
				// }];

				// #ifdef H5-DINGDING
				this.$deviceApi.setMenu(menuItemStr, res => {
					console.log(res);
					// this.setRightMeun();
					// if (res.id == 1) {
					//    this.handlerShowSearch();
					//  }
				});
				// #endif

			},
			chooseWebview(){
				this.$u.route({
					url: '/pages/commonPage/thirdPartyDraw',
				});
			},
			
			updateBackState(param) {
				console.log("111",param);
				if(param.getSignImg){
					this.base64 = param.getSignImg.base64;
				} else if(param.thirdPartyDraw) {
					this.base64 = param.thirdPartyDraw.base64;
				}
				
				
			}
		},
	};
</script>

<style></style>
